<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;

class UniversalExport implements FromCollection
{
    // 要导出的数据
    public $data;
    public $title;
    // 总行数
    public $rowNum;

    public function __construct($data, $title)
    {
        $this->data = $data;
        $this->title = $title;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $data = [];
        if (!empty($this->data)) {
            foreach ($this->data as $key => $vo) {
                foreach ($vo as $k => $v) {
                    $data[$key][$k] = " " . $v;
                }
            }
        }
        $headings = $this->title;
        array_unshift($data, $headings);
        $this->rowNum = count($data);
        // 此处数据需要数组转集合
        return collect($data);
    }
}
